﻿namespace YidanSoft.Library.EditorUtility.CommandBars
{
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.Win32;

    public class ToolBarItem
    {
        private EventHandler Changed;
        private bool check;
        private EventHandler Click;
        private EventHandler clickHandler;
        private System.Windows.Forms.ComboBox comboBox;
        private CommandBarMenu commandBarMenu;
        private EventHandler DropDown;
        private bool dropped;
        private bool enabled;
        private System.Drawing.Image image;
        private int index;
        private MenuItem[] menuItems;
        private ToolBarEx parentToolBar;
        private bool raiseEvents;
        private Keys shortCut;
        private ToolBarItemStyle style;
        private object tag;
        private string text;
        private string toolTip;
        private bool visible;

        public event EventHandler Changed
        {
            add
            {
                EventHandler handler2;
                EventHandler changed = this.Changed;
                do
                {
                    handler2 = changed;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    changed = Interlocked.CompareExchange<EventHandler>(ref this.Changed, handler3, handler2);
                }
                while (changed != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler changed = this.Changed;
                do
                {
                    handler2 = changed;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    changed = Interlocked.CompareExchange<EventHandler>(ref this.Changed, handler3, handler2);
                }
                while (changed != handler2);
            }
        }

        public event EventHandler Click
        {
            add
            {
                EventHandler handler2;
                EventHandler click = this.Click;
                do
                {
                    handler2 = click;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    click = Interlocked.CompareExchange<EventHandler>(ref this.Click, handler3, handler2);
                }
                while (click != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler click = this.Click;
                do
                {
                    handler2 = click;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    click = Interlocked.CompareExchange<EventHandler>(ref this.Click, handler3, handler2);
                }
                while (click != handler2);
            }
        }

        public event EventHandler DropDown
        {
            add
            {
                EventHandler handler2;
                EventHandler dropDown = this.DropDown;
                do
                {
                    handler2 = dropDown;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    dropDown = Interlocked.CompareExchange<EventHandler>(ref this.DropDown, handler3, handler2);
                }
                while (dropDown != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler dropDown = this.DropDown;
                do
                {
                    handler2 = dropDown;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    dropDown = Interlocked.CompareExchange<EventHandler>(ref this.DropDown, handler3, handler2);
                }
                while (dropDown != handler2);
            }
        }

        public ToolBarItem()
        {
            this.image = null;
            this.text = string.Empty;
            this.enabled = true;
            this.check = false;
            this.visible = true;
            this.dropped = false;
            this.raiseEvents = true;
            this.shortCut = Keys.None;
            this.style = ToolBarItemStyle.PushButton;
            this.toolTip = null;
            this.tag = null;
            this.clickHandler = null;
            this.comboBox = null;
            this.index = -1;
            this.parentToolBar = null;
            this.menuItems = null;
            this.commandBarMenu = null;
        }

        public ToolBarItem(string text)
        {
            this.image = null;
            this.text = string.Empty;
            this.enabled = true;
            this.check = false;
            this.visible = true;
            this.dropped = false;
            this.raiseEvents = true;
            this.shortCut = Keys.None;
            this.style = ToolBarItemStyle.PushButton;
            this.toolTip = null;
            this.tag = null;
            this.clickHandler = null;
            this.comboBox = null;
            this.index = -1;
            this.parentToolBar = null;
            this.menuItems = null;
            this.commandBarMenu = null;
            this.Initialize(null, text, null, Keys.None, null);
        }

        public ToolBarItem(System.Drawing.Image image, EventHandler clickHandler)
        {
            this.image = null;
            this.text = string.Empty;
            this.enabled = true;
            this.check = false;
            this.visible = true;
            this.dropped = false;
            this.raiseEvents = true;
            this.shortCut = Keys.None;
            this.style = ToolBarItemStyle.PushButton;
            this.toolTip = null;
            this.tag = null;
            this.clickHandler = null;
            this.comboBox = null;
            this.index = -1;
            this.parentToolBar = null;
            this.menuItems = null;
            this.commandBarMenu = null;
            this.Initialize(image, null, clickHandler, Keys.None, null);
        }

        public ToolBarItem(string text, EventHandler clickHandler)
        {
            this.image = null;
            this.text = string.Empty;
            this.enabled = true;
            this.check = false;
            this.visible = true;
            this.dropped = false;
            this.raiseEvents = true;
            this.shortCut = Keys.None;
            this.style = ToolBarItemStyle.PushButton;
            this.toolTip = null;
            this.tag = null;
            this.clickHandler = null;
            this.comboBox = null;
            this.index = -1;
            this.parentToolBar = null;
            this.menuItems = null;
            this.commandBarMenu = null;
            this.Initialize(null, text, clickHandler, Keys.None, null);
        }

        public ToolBarItem(ToolBarItemStyle style, System.Windows.Forms.ComboBox comboBox)
        {
            this.image = null;
            this.text = string.Empty;
            this.enabled = true;
            this.check = false;
            this.visible = true;
            this.dropped = false;
            this.raiseEvents = true;
            this.shortCut = Keys.None;
            this.style = ToolBarItemStyle.PushButton;
            this.toolTip = null;
            this.tag = null;
            this.clickHandler = null;
            this.comboBox = null;
            this.index = -1;
            this.parentToolBar = null;
            this.menuItems = null;
            this.commandBarMenu = null;
            Debug.Assert(comboBox != null);
            Debug.Assert(style == ToolBarItemStyle.ComboBox);
            this.style = style;
            this.comboBox = comboBox;
        }

        public ToolBarItem(System.Drawing.Image image, EventHandler clickHandler, Keys shortCut)
        {
            this.image = null;
            this.text = string.Empty;
            this.enabled = true;
            this.check = false;
            this.visible = true;
            this.dropped = false;
            this.raiseEvents = true;
            this.shortCut = Keys.None;
            this.style = ToolBarItemStyle.PushButton;
            this.toolTip = null;
            this.tag = null;
            this.clickHandler = null;
            this.comboBox = null;
            this.index = -1;
            this.parentToolBar = null;
            this.menuItems = null;
            this.commandBarMenu = null;
            this.Initialize(image, null, clickHandler, shortCut, null);
        }

        public ToolBarItem(System.Drawing.Image image, EventHandler clickHandler, Keys shortCut, string toolTip)
        {
            this.image = null;
            this.text = string.Empty;
            this.enabled = true;
            this.check = false;
            this.visible = true;
            this.dropped = false;
            this.raiseEvents = true;
            this.shortCut = Keys.None;
            this.style = ToolBarItemStyle.PushButton;
            this.toolTip = null;
            this.tag = null;
            this.clickHandler = null;
            this.comboBox = null;
            this.index = -1;
            this.parentToolBar = null;
            this.menuItems = null;
            this.commandBarMenu = null;
            this.Initialize(image, null, clickHandler, shortCut, toolTip);
        }

        public ToolBarItem(System.Drawing.Image image, string text, EventHandler clickHandler, System.Windows.Forms.Shortcut shortcut)
        {
            this.image = null;
            this.text = string.Empty;
            this.enabled = true;
            this.check = false;
            this.visible = true;
            this.dropped = false;
            this.raiseEvents = true;
            this.shortCut = Keys.None;
            this.style = ToolBarItemStyle.PushButton;
            this.toolTip = null;
            this.tag = null;
            this.clickHandler = null;
            this.comboBox = null;
            this.index = -1;
            this.parentToolBar = null;
            this.menuItems = null;
            this.commandBarMenu = null;
            this.Initialize(image, text, clickHandler, this.shortCut, null);
        }

        public ToolBarItem(System.Drawing.Image image, string text, EventHandler clickHandler, Keys shortCut, string toolTip)
        {
            this.image = null;
            this.text = string.Empty;
            this.enabled = true;
            this.check = false;
            this.visible = true;
            this.dropped = false;
            this.raiseEvents = true;
            this.shortCut = Keys.None;
            this.style = ToolBarItemStyle.PushButton;
            this.toolTip = null;
            this.tag = null;
            this.clickHandler = null;
            this.comboBox = null;
            this.index = -1;
            this.parentToolBar = null;
            this.menuItems = null;
            this.commandBarMenu = null;
            this.Initialize(image, text, clickHandler, shortCut, toolTip);
        }

        private void Initialize(System.Drawing.Image image, string text, EventHandler clickHandler, Keys shortCut, string toolTip)
        {
            this.image = image;
            this.text = text;
            if (clickHandler != null)
            {
                this.Click += clickHandler;
                this.clickHandler = clickHandler;
            }
            this.shortCut = shortCut;
            this.toolTip = toolTip;
        }

        private void RaiseChanged()
        {
            if ((this.Changed != null) && this.raiseEvents)
            {
                this.Changed(this, EventArgs.Empty);
            }
        }

        internal void RaiseClick()
        {
            if ((this.Click != null) && this.raiseEvents)
            {
                this.Click(this, EventArgs.Empty);
            }
        }

        internal void RaiseDropDown()
        {
            if ((this.DropDown != null) && this.raiseEvents)
            {
                this.DropDown(this, EventArgs.Empty);
            }
        }

        public bool Checked
        {
            get
            {
                return this.check;
            }
            set
            {
                if (this.check != value)
                {
                    this.check = value;
                    this.RaiseChanged();
                }
            }
        }

        public EventHandler ClickHandler
        {
            get
            {
                return this.clickHandler;
            }
            set
            {
                if (this.clickHandler != value)
                {
                    this.clickHandler = value;
                    this.RaiseChanged();
                }
            }
        }

        public System.Windows.Forms.ComboBox ComboBox
        {
            get
            {
                return this.comboBox;
            }
        }

        public bool Dropped
        {
            get
            {
                return this.dropped;
            }
            set
            {
                this.dropped = value;
            }
        }

        public bool Enabled
        {
            get
            {
                return this.enabled;
            }
            set
            {
                if (this.enabled != value)
                {
                    this.enabled = value;
                    if (this.ComboBox != null)
                    {
                        this.ComboBox.Enabled = value;
                    }
                    this.RaiseChanged();
                }
            }
        }

        public System.Drawing.Image Image
        {
            get
            {
                return this.image;
            }
            set
            {
                if (this.image != value)
                {
                    this.image = value;
                    this.RaiseChanged();
                }
            }
        }

        public int Index
        {
            get
            {
                return this.index;
            }
            set
            {
                this.index = value;
            }
        }

        public Rectangle ItemRectangle
        {
            get
            {
                Debug.Assert(this.parentToolBar != null);
                YidanSoft.Library.EditorUtility.Win32.RECT lParam = new YidanSoft.Library.EditorUtility.Win32.RECT();
                WindowsAPI.SendMessage(this.parentToolBar.Handle, 0x433, this.index, ref lParam);
                return new Rectangle(lParam.left, lParam.top, lParam.right - lParam.left, lParam.bottom - lParam.top);
            }
        }

        public MenuItem[] MenuItems
        {
            get
            {
                return this.menuItems;
            }
            set
            {
                if (this.menuItems != value)
                {
                    this.menuItems = value;
                    this.commandBarMenu = new CommandBarMenu(this.menuItems);
                    this.RaiseChanged();
                }
            }
        }

        internal bool RaiseEvents
        {
            get
            {
                return this.raiseEvents;
            }
            set
            {
                this.raiseEvents = value;
            }
        }

        public Keys Shortcut
        {
            get
            {
                return this.shortCut;
            }
            set
            {
                if (this.shortCut != value)
                {
                    this.shortCut = value;
                    this.RaiseChanged();
                }
            }
        }

        public ToolBarItemStyle Style
        {
            get
            {
                return this.style;
            }
            set
            {
                if (this.style != value)
                {
                    this.style = value;
                    this.RaiseChanged();
                }
            }
        }

        public object Tag
        {
            get
            {
                return this.tag;
            }
            set
            {
                this.tag = value;
            }
        }

        public string Text
        {
            get
            {
                return this.text;
            }
            set
            {
                if (this.text != value)
                {
                    this.text = value;
                    this.RaiseChanged();
                }
            }
        }

        public ToolBarEx ToolBar
        {
            set
            {
                this.parentToolBar = value;
            }
        }

        public CommandBarMenu ToolBarItemMenu
        {
            get
            {
                return this.commandBarMenu;
            }
        }

        public string ToolTip
        {
            get
            {
                return this.toolTip;
            }
            set
            {
                if (this.toolTip != value)
                {
                    this.toolTip = value;
                    this.RaiseChanged();
                }
            }
        }

        public bool Visible
        {
            get
            {
                return this.visible;
            }
            set
            {
                if (this.visible != value)
                {
                    this.visible = value;
                    this.RaiseChanged();
                }
            }
        }
    }
}

